#ifndef MO_IMAGE_RECTIFY_H
#define MO_IMAGE_RECTIFY_H

#include "MoTypes.h"

#include <vector>

#define IN
#define OUT

#ifdef __cplusplus
extern "C" {
    #define DLL_API_C_DECL extern "C" __attribute__((visibility("default")))
#endif

// version 1
DLL_API_C_DECL int moInitImgRectifyModule(MoFullStereoCameraParams *pParam);
DLL_API_C_DECL void moReleaseImgRectifyModule();

DLL_API_C_DECL int moRectifyImage(IN MoFrame *pInImg, OUT MoFrame *pRectifiedImg);

DLL_API_C_DECL int moUndistortRGBPoints2DispSpace(IN std::vector<MoPoint2D> RgbPts, OUT std::vector<MoPoint2D> &DispPts, IN MoPoint2D offset = MoPoint2D{-0.6,0});
DLL_API_C_DECL int moProjectDispPoints2RGBSpace(IN std::vector<MoPoint2D> DispPts, OUT std::vector<MoPoint2D> &RgbPts);
DLL_API_C_DECL int moUndistortRGBRect2DispSpace(IN MoRect &RGBRect, OUT std::vector<MoPoint2D> &DispPts);
DLL_API_C_DECL int moProjectDispRect2RGBSpace(IN MoRect &DispRect, OUT std::vector<MoPoint2D> &RgbPts);


// version 2
DLL_API_C_DECL int  moInitImgRectifyModuleV2();
DLL_API_C_DECL void moReleaseImgRectifyModuleV2();

/**
 * @brief Recitify isp right image to simor reference image equivalent. Rectified isp img is matched up with disparity
 * data or simor reference image.
 * call moSplitRGBFrame/moLocalSplitRGBFrame with rgb_frame(isp_frame) to get _isp_right_img
 * _simor_ref_img_equivalent is same as _rectified_isp_right_img here
 * @param _isp_right_img 
 * @param _simor_ref_img_equivalent
 * @return int
 */
DLL_API_C_DECL int moRectifyIspRightImgToSimorRefImgV2(IN MoFrame* _isp_right_img, OUT MoFrame *_rectified_simor_ref_img_equivalent);

/**
 * @brief  Undistort image points on ISP image to points on disparity/simor_rectified img
 *
 * @param _v_isp_pts
 * @param _v_dsp_pts
 * @return int
 */
DLL_API_C_DECL int moUndistortIspPtsToDspSpaceV2(IN const std::vector<MoPoint2D>& _v_isp_pts, OUT std::vector<MoPoint2D> &_v_dsp_pts, IN MoPoint2D offset = MoPoint2D{-0.6,0});

/**
 * @brief: Same as undistortIspPtsToDspSpaceV2. Take rectangle roi as input for convenience
 *
 * @param _isp_roi
 * @param _v_dsp_pts
 * @return int
 */
DLL_API_C_DECL int moUndistortIspROItoDspSpaceV2(IN const MoRect& _isp_roi, OUT std::vector<MoPoint2D> &_v_dsp_pts);

/**
 * @brief Project points on disparity/simor_rectified img to points on ISP image
 *
 * @param _p_isp_simor_matchup_handleV2
 * @param _v_dsp_pts
 * @param _v_isp_pts
 * @return int
 */
DLL_API_C_DECL int moProjectDspPtsToIspImgSpaceV2(IN const std::vector<MoPoint2D>& _v_dsp_pts, OUT std::vector<MoPoint2D> &_v_isp_pts);

/**
 * @brief Same as projectDspPtsToIspImgSpaceV2. Take rectangle roi as input for convenience. All points in the roi will
 * be calculated.
 *
 * @param _dsp_roi
 * @param _v_isp_pts
 * @return int
 */
DLL_API_C_DECL int moProjectDspRoiToIspImgSpaceV2(IN const MoRect& _dsp_roi, OUT std::vector<MoPoint2D> &_v_isp_pts);

/**
 * @brief Rectify ISP Based on recommended ISP rectify parameter.
 *        Original ISP img:640x480 -> Rectified ISP image: 640x480
 *        call moSplitRGBFrame/moLocalSplitRGBFrame with rgb_frame(isp_frame) to get _isp_left_ori and _isp_right_ori image
 * 
 * IMPORTANT NOTE:
 *        This rectified ISP image is completedly irrelevant with simor reference image(See
 * rectifyIspRightImgToSimorRefImgV2)
 * 
 * @param _isp_left_ori
 * @param _isp_right_ori
 * @param _isp_left_rect
 * @param _isp_right_rect
 * @return int
 */
DLL_API_C_DECL int moRectifyOriginalIspImageV2(IN MoFrame *_isp_left_ori, IN MoFrame *_isp_right_ori,
                              OUT MoFrame *_isp_left_rect, OUT MoFrame *_isp_right_rect);

#ifdef __cplusplus
}
#endif

#endif
